<?php
/**
 * Process displaying data for user profile
 */
class ProfileController extends AppController {
	function beforeFilter() {
		parent::beforeFilter();
		$this->CustomAuth->allow('view');
	}

	/**
	 * Action display profile of login user
	 *
	 * @param $id
	 */
	function view($id) {
		$this->loadModel('Course');
		$this->loadModel('User');
		$this->loadModel('TargetOfWatch');
		$user = $this->User->read(null, $id);
		if (!$user) {
			$this->cakeError('error404');
		}
		$options = $this->Course->getPaginatorOption();
		$options['conditions']['Course.user_id'] = $id;
		if ($this->CustomAuth->user('id') == $id) {
			unset($options['conditions']['Course.display_flg']);
		}
		$this->paginate = $options;
		$courses = $this->paginate('Course');

		$ids = array();
		foreach ($courses as $course) {
			$ids[] = $course['Course']['id'];
		}
		$details = $this->Course->getClosestCourseDetail($ids);
		$watchListInfo = $this->TargetOfWatch->getTargetOfWatchInformationByUserIdAndTeacherId($this->CustomAuth->user('id'), $id);
		if ($user['User']['id'] == $this->CustomAuth->user('id')) {
			$isOwner = true;
		} else {
			$isOwner = false;
		}

		$this->set(array(
			'courses' => $courses,
			'details' => $details,
			'user' => $user,
			'isOwner' => $isOwner,
			'nbEnteredCourse' => $this->_getEnteredClassNumber($id),
			'watchListInfo' => $watchListInfo,
			'categoryConfig' => Configure::read('category'),
		));
	}
}